package com.example.neusoft.a16110100126yttordering.activities;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.neusoft.a16110100126yttordering.Adapter.FoodListAdapter;
import com.example.neusoft.a16110100126yttordering.R;
import com.example.neusoft.a16110100126yttordering.bean.CollectStatus;
import com.example.neusoft.a16110100126yttordering.bean.FoodInfo;
import com.example.neusoft.a16110100126yttordering.bean.Result;
import com.example.neusoft.a16110100126yttordering.common.Constants;
import com.example.neusoft.a16110100126yttordering.listener.RetrofitListener;
import com.example.neusoft.a16110100126yttordering.model.FoodModel;
import com.squareup.picasso.Picasso;

import java.util.List;


public class FoodListActivity extends BaseActivity implements RetrofitListener {
    private int Shop_id;
    private ImageButton imageButton_back;
    private ImageButton imageButton_collection;
    private ImageView imageView_head;
    private ImageView imageView_address;
    private TextView textView_back;
    private TextView textView_shopname;
    private TextView textView_address;
    private RecyclerView recyclerView;
    private FoodModel foodModel;
    private FoodListAdapter adapter;
    private String result="1";


    @Override
    void initViews() {
        setLayout(R.layout.activity_food_list);
        imageButton_back=findViewById(R.id.imageButton_food);
        imageButton_collection=findViewById(R.id.imageButton_collection);
        imageView_address=findViewById(R.id.imageView_food);
        imageView_head=findViewById(R.id.food_head);
        textView_address=findViewById(R.id.textView2_food);
        textView_back=findViewById(R.id.textView1_food);
        textView_shopname=findViewById(R.id.textView_food);
        Intent intent=getIntent();
        Shop_id=intent.getIntExtra("shop_id",1);
        textView_shopname.setText(intent.getStringExtra("shop_name"));
        Picasso.get().load(Constants.BASE_URL+intent.getStringExtra("shop_pic")).into(imageView_head);
        recyclerView=findViewById(R.id.recycleView4);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }


//    @Override
//    public void onBackPressed() {
//        super.onBackPressed();
//        Intent intent=new Intent();
//        intent.setClass(FoodListActivity.this,MainActivity.class);
//        startActivity(intent);
//    }

    @Override
    void initEvents() {
        imageButton_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setClass(FoodListActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });
        imageButton_collection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                collect();
            }
        });
    }

    @Override
    void initData() {
        getFoodList();
        iscollected();
    }

    private void getFoodList(){
            foodModel=new FoodModel();
            foodModel.getFoodByShop(Shop_id,this);

}
    private void iscollected(){
        foodModel=new FoodModel();
        foodModel.isCollected(getUser_id(),Shop_id,0,this);
}
    private void collect(){
        foodModel=new FoodModel();
        foodModel.userCollectShop(getUser_id(),Shop_id,this);
    }
    @Override
    public void onSuccess(Object o, int flag) {
            switch (flag){
                case Constants.GETFOODBYSHOP:
                    adapter=new FoodListAdapter(FoodListActivity.this,(List<FoodInfo>)o,R.layout.item1);
                    recyclerView.setAdapter(adapter);
                    break;
                case Constants.ISCOLLECTED:
                    result=((Result)o).getCollected();

                    if (result.equals("0")){
                        imageButton_collection.setImageResource(R.drawable.ic_love2);

                    }else {
                        imageButton_collection.setImageResource(R.drawable.ic_love1);

                    }
                    break;
                case Constants.USERCOLLECTSHOP:
                    if(((Result)o).getSuccess().equals("1")){
                        if(result.equals("0")){
                            imageButton_collection.setImageResource(R.drawable.ic_love1);
                            Toast.makeText(FoodListActivity.this,"收藏成功",Toast.LENGTH_SHORT).show();
                            result="1";
                        }else {
                            imageButton_collection.setImageResource(R.drawable.ic_love2);
                            Toast.makeText(FoodListActivity.this,"取消收藏成功",Toast.LENGTH_SHORT).show();
                            result="0";
                        }
                    }else {
                        Toast.makeText(FoodListActivity.this,"操作失败",Toast.LENGTH_SHORT).show();
                    }
                    break;

            }
    }

    @Override
    public void onFail() {

        showToast("网络连接错误");
    }
}
